using AutoMapper;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ShopPowerTemplate.Entities;
using ShopPowerTemplate.IoC;
using ShopPowerTemplate.Presentation.Mvc.Controllers;
using ShopPowerTemplate.Presentation.Mvc.Extensions.BootStrapper;
using ShopPowerTemplate.Presentation.Mvc.ViewModels;
using ShopPowerTemplate.Services.Tests;
using System.Web.Mvc;

namespace ShopPowerTemplate.Presentation.Mvc.Test
{
    [TestClass]
    public class CustomerControllerTestBase
    {
        [TestInitialize]
        public virtual void TestInitialize()
        {
            IBootStrapper bootStrapper = new DefaultBootStrapper(IoCFactory.Instance.CurrentContainer);
            bootStrapper.Boot();
        }

        [TestMethod]
        public virtual void CustomerController_IndexTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CustomerController>());

            var result = controller.Index() as ViewResult;
            
            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
            Assert.IsTrue(result.Model is IndexCustomerViewModel);
        }

        [TestMethod]
        public virtual void CustomerController_LoadTest()
        {
            CustomerServiceUtil.CreateCustomer();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CustomerController>());

            var result = controller.Load("", "", 0, 10, "") as JsonResult;

            var rows = ModelHelper.GetList(result.Data);
            Assert.IsTrue(rows.Count > 0);
        }

        [TestMethod]
        public virtual void CustomerController_DeleteTest()
        {
            Customer customer = CustomerServiceUtil.CreateCustomer();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CustomerController>());

            var result = controller.Delete(customer.Id) as ContentResult;

            Assert.IsNotNull(result);
            Assert.IsTrue(string.IsNullOrEmpty(result.Content));
        }

        [TestMethod]
        public virtual void CustomerController_ReportTest()
        {
            Customer customer = CustomerServiceUtil.CreateCustomer();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CustomerController>());

            var result = controller.Report() as FileContentResult;

            Assert.IsNotNull(result);
            Assert.IsTrue(result.ContentType == "text/csv");
            Assert.IsNotNull(result.FileContents.Length > 0);
        }

        [TestMethod]
        public virtual void CustomerController_GetCreateTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CustomerController>());

            var result = controller.Create() as ViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
            Assert.IsTrue(result.Model is CreateCustomerViewModel);
        }

        [TestMethod]
        public virtual void CustomerController_PostCreateTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CustomerController>());
            Customer customer = CustomerServiceUtil.GetCustomer();
            CreateCustomerViewModel viewModel = Mapper.Map<Customer, CreateCustomerViewModel>(customer);

            var result = controller.Create(viewModel) as RedirectToRouteResult;

            Assert.IsNotNull(result);
            Assert.AreEqual(result.RouteValues["action"], "Edit"); //Create redirect to edit action on success
            Assert.IsTrue((int)result.RouteValues["id"] > 0); //Check new item id
        }

        [TestMethod]
        public virtual void CustomerController_GetEditTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CustomerController>());
            Customer customer = CustomerServiceUtil.CreateCustomer();

            var result = controller.Edit(customer.Id) as ViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
            Assert.IsTrue(result.Model is EditCustomerViewModel);
            Assert.AreEqual((result.Model as EditCustomerViewModel).FirstName, customer.FirstName);
        }

        [TestMethod]
        public virtual void CustomerController_PostEditTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CustomerController>());
            Customer customer = CustomerServiceUtil.CreateCustomer();
            customer.FirstName = "Test";

            EditCustomerViewModel viewModel = Mapper.Map<Customer, EditCustomerViewModel>(customer);
            controller.Edit(viewModel);

            var result = controller.Edit(customer.Id) as ViewResult;
            Assert.IsNotNull(result);
            Assert.AreEqual((result.Model as EditCustomerViewModel).FirstName, "Test");
        }

        [TestMethod]
        public virtual void CustomerController_DetailTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CustomerController>());
            Customer customer = CustomerServiceUtil.CreateCustomer();

            var result = controller.Details(customer.Id) as ViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
            Assert.IsTrue(result.Model is DetailsCustomerViewModel);
            Assert.AreEqual((result.Model as DetailsCustomerViewModel).FirstName, customer.FirstName);
        }

		/* Details region test */
        [TestMethod]
        public virtual void CustomerController_LoadReviewsTest()
        {
            Review reviews = ReviewServiceUtil.CreateReviewFull();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CustomerController>());

            var result = controller.LoadReviews("", "", 0, 10, "", 1, reviews.Customer.Id) as JsonResult;

            var rows = ModelHelper.GetList(result.Data);
            Assert.IsTrue(rows.Count > 0);
        }
        
        [TestMethod]
        public virtual void CustomerController_AddReviewsTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CustomerController>());
            Review reviews = ReviewServiceUtil.GetReviewFull();
            PopUpReviewViewModel viewModel = Mapper.Map<Review, PopUpReviewViewModel>(reviews);

            var result = controller.AddReviews(viewModel) as ContentResult;

            Assert.IsNotNull(result);
            Assert.AreEqual(result.Content, "OK");
        }

        [TestMethod]
        public virtual void CustomerController_GetEditReviewsTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CustomerController>());
            Review reviews = ReviewServiceUtil.CreateReviewFull();

            var result = controller.EditReviews(reviews.Id, reviews.Customer.Id) as PartialViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.AreEqual(result.ViewName, "~/Views/Review/PopUp.cshtml");
            Assert.IsTrue(result.Model is PopUpReviewViewModel);
			Assert.AreEqual((result.Model as PopUpReviewViewModel).Comment, reviews.Comment);
            
        }

        [TestMethod]
        public virtual void CustomerController_PostEditReviewsTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CustomerController>());
            Review reviews = ReviewServiceUtil.CreateReviewFull();
            reviews.Comment = "Test";

            PopUpReviewViewModel viewModel = Mapper.Map<Review, PopUpReviewViewModel>(reviews);
            controller.EditReviews(viewModel);

            var result = controller.EditReviews(reviews.Id) as PartialViewResult;
            Assert.IsNotNull(result);
			Assert.AreEqual((result.Model as PopUpReviewViewModel).Comment, "Test");
        }

        [TestMethod]
        public virtual void CustomerController_DeleteReviewsTest()
        {
            Review reviews = ReviewServiceUtil.CreateReviewFull();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CustomerController>());

            controller.DeleteReviews(reviews.Id, reviews.Customer.Id);

            Assert.AreEqual(controller.Response.StatusCode, 200);
        }

		/* Details region test */
        [TestMethod]
        public virtual void CustomerController_LoadOrdersTest()
        {
            Order orders = OrderServiceUtil.CreateOrderFull();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CustomerController>());

            var result = controller.LoadOrders("", "", 0, 10, "", 1, orders.Customer.Id) as JsonResult;

            var rows = ModelHelper.GetList(result.Data);
            Assert.IsTrue(rows.Count > 0);
        }
        
        [TestMethod]
        public virtual void CustomerController_AddOrdersTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CustomerController>());
            Order orders = OrderServiceUtil.GetOrderFull();
            PopUpOrderViewModel viewModel = Mapper.Map<Order, PopUpOrderViewModel>(orders);

            var result = controller.AddOrders(viewModel) as ContentResult;

            Assert.IsNotNull(result);
            Assert.AreEqual(result.Content, "OK");
        }

        [TestMethod]
        public virtual void CustomerController_GetEditOrdersTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CustomerController>());
            Order orders = OrderServiceUtil.CreateOrderFull();

            var result = controller.EditOrders(orders.Id, orders.Customer.Id) as PartialViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.AreEqual(result.ViewName, "~/Views/Order/PopUp.cshtml");
            Assert.IsTrue(result.Model is PopUpOrderViewModel);
			Assert.AreEqual((result.Model as PopUpOrderViewModel).Reference, orders.Reference);
            
        }

        [TestMethod]
        public virtual void CustomerController_PostEditOrdersTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CustomerController>());
            Order orders = OrderServiceUtil.CreateOrderFull();
            orders.Reference = "Test";

            PopUpOrderViewModel viewModel = Mapper.Map<Order, PopUpOrderViewModel>(orders);
            controller.EditOrders(viewModel);

            var result = controller.EditOrders(orders.Id) as PartialViewResult;
            Assert.IsNotNull(result);
			Assert.AreEqual((result.Model as PopUpOrderViewModel).Reference, "Test");
        }

        [TestMethod]
        public virtual void CustomerController_DeleteOrdersTest()
        {
            Order orders = OrderServiceUtil.CreateOrderFull();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CustomerController>());

            controller.DeleteOrders(orders.Id, orders.Customer.Id);

            Assert.AreEqual(controller.Response.StatusCode, 200);
        }

		/* Details region test */
        [TestMethod]
        public virtual void CustomerController_LoadAddressesTest()
        {
            Address addresses = AddressServiceUtil.CreateAddressFull();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CustomerController>());

            var result = controller.LoadAddresses("", "", 0, 10, "", 1, addresses.Customer.Id) as JsonResult;

            var rows = ModelHelper.GetList(result.Data);
            Assert.IsTrue(rows.Count > 0);
        }
        
        [TestMethod]
        public virtual void CustomerController_AddAddressesTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CustomerController>());
            Address addresses = AddressServiceUtil.GetAddressFull();
            PopUpAddressViewModel viewModel = Mapper.Map<Address, PopUpAddressViewModel>(addresses);

            var result = controller.AddAddresses(viewModel) as ContentResult;

            Assert.IsNotNull(result);
            Assert.AreEqual(result.Content, "OK");
        }

        [TestMethod]
        public virtual void CustomerController_GetEditAddressesTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CustomerController>());
            Address addresses = AddressServiceUtil.CreateAddressFull();

            var result = controller.EditAddresses(addresses.Id, addresses.Customer.Id) as PartialViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.AreEqual(result.ViewName, "~/Views/Address/PopUp.cshtml");
            Assert.IsTrue(result.Model is PopUpAddressViewModel);
			Assert.AreEqual((result.Model as PopUpAddressViewModel).AddressLine1, addresses.AddressLine1);
            
        }

        [TestMethod]
        public virtual void CustomerController_PostEditAddressesTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CustomerController>());
            Address addresses = AddressServiceUtil.CreateAddressFull();
            addresses.AddressLine1 = "Test";

            PopUpAddressViewModel viewModel = Mapper.Map<Address, PopUpAddressViewModel>(addresses);
            controller.EditAddresses(viewModel);

            var result = controller.EditAddresses(addresses.Id) as PartialViewResult;
            Assert.IsNotNull(result);
			Assert.AreEqual((result.Model as PopUpAddressViewModel).AddressLine1, "Test");
        }

        [TestMethod]
        public virtual void CustomerController_DeleteAddressesTest()
        {
            Address addresses = AddressServiceUtil.CreateAddressFull();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CustomerController>());

            controller.DeleteAddresses(addresses.Id, addresses.Customer.Id);

            Assert.AreEqual(controller.Response.StatusCode, 200);
        }

    }
}

